#include "dxManager.h"
#include "Game.h"
#include <windows.h>

HINSTANCE hInst;
HWND wndHandle;
char* BlockFiles[] = {"Line.bmp", "Square.bmp", "LBlock.bmp"};

dxManager Dx;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
bool initWindow(HINSTANCE hInstance);

LARGE_INTEGER timeStart;
LARGE_INTEGER timeEnd;
LARGE_INTEGER timerFreq;	//How fast the clock is in the system
float         anim_rate;	//What speed its going to move at

bool initWindow(HINSTANCE hInstance);
bool initSprites(void);
void drawSprite();

#define MAX_SPRITES 1			// this is the number of sprites we want

#define MAX_LOCX (640- 64)		// don't want the fish to go off the screen
#define MAX_LOCY (480 - 64)

int initSpeed;

LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{

	if (!initWindow(hInstance))
		return false;

	if (!Dx.init(wndHandle))
		return false;

	if (!initSprites())	
		return false;

	// Main message loop:
	// Enter the message loop
    MSG msg; 
    ZeroMemory( &msg, sizeof(msg) );

	Game g(6,0,"Map.txt", BlockFiles);
	QueryPerformanceFrequency(&timerFreq);

    while( msg.message!=WM_QUIT )
    {
		if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
        {
			TranslateMessage( &msg );
            DispatchMessage( &msg );
        }
		else
		{
			QueryPerformanceCounter(&timeStart);
			Dx.beginRender();
				
			
			Dx.endRender();
			QueryPerformanceCounter(&timeEnd);

			//How many miliseconds that have passed
			anim_rate = ( (float)timeEnd.QuadPart - (float)timeStart.QuadPart ) / timerFreq.QuadPart;
			//Sleep(5000);

		}
    }
	
	Dx.shutdown();

	return (int) msg.wParam;
}

bool initWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= 0;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= "DirectXExample";
	wcex.hIconSm		= 0;
	RegisterClassEx(&wcex);

	wndHandle = CreateWindow("DirectXExample", 
							 "Ectoplasmosis", 
							 WS_OVERLAPPEDWINDOW,
							 CW_USEDEFAULT, 
							 CW_USEDEFAULT, 
							 640, 
							 480, 
							 NULL, 
							 NULL, 
							 hInstance, 
							 NULL);
   if (!wndHandle)
      return false;
   
   ShowWindow(wndHandle, SW_SHOW);
   UpdateWindow(wndHandle);

   return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message) 
	{
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}
	return DefWindowProc(hWnd, message, wParam, lParam);
}

bool initSprites(void)
{
	spriteSurface = Dx.getSurfaceFromBitmap("Line.bmp", 10, 50);
	if (spriteSurface == NULL)
		return false;

		//defines the sprite
		spriteStruct.srcRect.top = 0;
		spriteStruct.srcRect.left = 0; 
		spriteStruct.srcRect.right = spriteStruct.srcRect.left + 10; 
		spriteStruct.srcRect.bottom =spriteStruct.srcRect.top + 50;
		spriteStruct.posX = 0;
		spriteStruct.posY = 0;
		spriteStruct.Direction = 1;
		
		// set the animation data
		spriteStruct.curFrame = 0;
		spriteStruct.numFrames = 4;

		// set the move data
		spriteStruct.moveX = initSpeed;
		spriteStruct.moveY = 0.0;

	return true;
}

void drawSprite()
{
	//// make the sprite animation through the frames
	//if (spriteStruct.curFrame < spriteStruct.numFrames-1)
	//	spriteStruct.curFrame++;
	//else
	//	spriteStruct.curFrame = 0;
	//
	//// set the source rect to the correct frame position
	//spriteStruct.srcRect.top = spriteStruct.curFrame * 40;
	//spriteStruct.srcRect.bottom = spriteStruct.srcRect.top + 40;
	//

	//// move the sprite by it's movement rate
	//spriteStruct.posX += spriteStruct.moveX * anim_rate;

	

	// make sure that this sprite is not going off the screen
	/*if (spriteStruct.posX > MAX_LOCX)
	{
		spriteStruct.moveX = -initSpeed;
		spriteStruct.Direction = 3;
	}
	else if (spriteStruct.posX < 0)
	{
		spriteStruct.moveX = initSpeed;
		spriteStruct.Direction = 1;
	}*/
	////Sprite Switching
	//if (spriteStruct.Direction == 1)
	//{
	//	spriteStruct.srcRect.top = 0;
	//}
	//else if (spriteStruct.Direction == 3)
	//{
	//	spriteStruct.srcRect.top = 23;
	//}
	//spriteStruct.srcRect.bottom = spriteStruct.srcRect.top + 23;

	// set the destination for this sprite
	RECT destRect;
	destRect.left = int(spriteStruct.posX);
	destRect.top = int(spriteStruct.posY);
	destRect.bottom = destRect.top + 50;
	destRect.right = destRect.left + 10;

	// draw the sprite to the back buffer
	Dx.blitToSurface(spriteSurface, &spriteStruct.srcRect, &destRect);
}

